<?php

//用户注册
include_once '../../../library/config.php';
include_once '../../../model/userlist.php';
include_once '../../../model/userinvitelist.php';

//邀请人
$entity = new UserList();
$list = new UserList();

$entity->request();

$invite = new UserInviteList();
$repeat = new UserInviteList();
$invitemyid = '';
try {

    if (preg_match("/^1[34578]\d{9}$/", $entity['inviter'])) {   //默认inviter是手机号,查询其对应myid
        $list->select("myid")->where("account=" . $entity['inviter'])->get_first_rows();  //查询邀请人手机号对应的myid
        $invitemyid = $list['myid'];
    }

//    if (mb_strlen($entity['inviter'] == 6)) {   //6位数字邀请码(邀请人的myid左侧补0)
//        $invitemyid = ltrim($entity['intiver'], '0');
//    } else {        //邀请人myid
//        $invitemyid = $entity['inviter'];
//    }
//    if (!$list->select("myid")->where("myid=" . $invitemyid)->get_first_rows()) {
//        throw new Exception("113");     //判断邀请人是否存在
//    }
    if ($repeat->select("myid")->where("myid=" . $entity['myid'])->get_first_rows()) {
        throw new Exception("114");     //判断是否已填写邀请人
    }
    $invite->keyvalue("myid", $entity['myid'])->keyvalue("invitemyid", $invitemyid);
    $invite->append()->submit();
} catch (Exception $ex) {
    $invite->rsscode($ex);
}
echo $invite->toJson();
